import com.google.maps.LatLng;
import com.google.maps.MapMouseEvent;
import com.google.maps.MapType;
import com.google.maps.controls.ZoomControl;
import com.google.maps.overlays.Marker;
import com.google.maps.overlays.MarkerOptions;
import com.google.maps.services.ClientGeocoder;
import com.google.maps.services.ClientGeocoderOptions;
import com.google.maps.services.GeocodingEvent;
import com.google.maps.styles.FillStyle;
import com.google.maps.styles.StrokeStyle;

private var geocoder:ClientGeocoder;

private function onLocationMapReady(event:Event):void
{
	locationMap.enableScrollWheelZoom();
	locationMap.enableContinuousZoom();
	locationMap.addControl(new ZoomControl());
	locationMap.clearOverlays();
	locationMap.addEventListener(MapMouseEvent.CLICK, onLocationMapClick);
	
	var marker:Marker = new Marker(
		new LatLng(50.503887, 4.469936), 
		new MarkerOptions({
			strokeStyle: new StrokeStyle({color: 0x3e3e3e}),
			fillStyle: new FillStyle({color: 0x0000ff, alpha: 0.8}),
			radius: 7,
			hasShadow: true
		})
	);
	locationMap.addOverlay(marker);
	
	geocoder = new ClientGeocoder();
	geocoder.setOptions( new ClientGeocoderOptions({language: 'en', countryCode: 'UK'}) );
	geocoder.addEventListener(GeocodingEvent.GEOCODING_SUCCESS, handleGeocoderSuccess);
	geocoder.addEventListener(GeocodingEvent.GEOCODING_FAILURE, handleGeocoderFailure);
}

private function handleGeocoderSuccess(event:GeocodingEvent):void
{
	selectCountry(event.response.placemarks[0].addressDetails.Country.CountryName);
}

private function handleGeocoderFailure(event:GeocodingEvent):void
{
	Alert.show("Unable to geocode address: " + event.name);
}

private function onLocationMapClick(event:MapMouseEvent):void
{
	locationMap.clearOverlays();
	var marker:Marker = new Marker(
		event.latLng, 
		new MarkerOptions({
			strokeStyle: new StrokeStyle({color: 0x3e3e3e}),
			fillStyle: new FillStyle({color: 0x0000ff, alpha: 0.8}),
			radius: 7,
			hasShadow: true
		})
	);
	locationMap.addOverlay(marker);
	geocoder.reverseGeocode(event.latLng);
}

private function selectCountry(country:String):void
{
	if(country=="USA") country = "United States";
	if(country=="Russian Federation") country = "Russia";
	
	var item:String;
    for (var i:int=0; i<countryBox.dataProvider.length; i++) {
    	item = countryBox.dataProvider[i].toString();
    	if(item == country){
    		countryBox.selectedIndex = i;
    		lblSelectedCountry.text = "Selected country: " + countryBox.text;
    		break;
    	}
    }
}

private function selectLocation(country:String):void
{
	locationMap.clearOverlays();
	
	var lat:Number = parseFloat(geocodes.Geocode.(Country == country).Lat[0].text());
	var lng:Number = parseFloat(geocodes.Geocode.(Country == country).Lng[0].text());
	
    var marker:Marker = new Marker(
    	new LatLng(lat,lng), 
    	new MarkerOptions({
			strokeStyle: new StrokeStyle({color: 0x3e3e3e}),
			fillStyle: new FillStyle({color: 0x0000ff, alpha: 0.8}),
			radius: 7,
			hasShadow: true
    	})
    );
    	
	locationMap.addOverlay(marker);     
}